<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智慧校园 - 请假单列表</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #4a90e2;
            --success-color: #2ecc71;
            --danger-color: #e74c3c;
            --text-color: #2c3e50;
            --border-color: #dcdde1;
            --background-color: #f5f6fa;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
            background: var(--background-color);
            color: var(--text-color);
            margin: 0;
            padding: 20px;
        }

        .dashboard {
            max-width: 1400px;
            margin: 0 auto;
        }

        /* 统计卡片样式 */
        .stats-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .stat-card {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            display: flex;
            align-items: center;
            transition: transform 0.3s ease;
        }

        .stat-card:hover {
            transform: translateY(-5px);
        }

        .stat-card i {
            font-size: 2.5em;
            color: var(--primary-color);
            margin-right: 20px;
        }

        .stat-info h3 {
            margin: 0;
            font-size: 0.9em;
            color: #666;
        }

        .stat-info p {
            margin: 5px 0 0;
            font-size: 1.8em;
            font-weight: bold;
            color: var(--primary-color);
        }

        /* 表格样式 */
        .table-responsive {
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            overflow: hidden;
            margin-bottom: 20px;
            padding: 20px;
        }

        .table {
            width: 100%;
            border-collapse: collapse;
        }

        .table th {
            background: #f8f9fa;
            padding: 15px;
            text-align: left;
            font-weight: 600;
            color: var(--text-color);
            border-bottom: 2px solid var(--border-color);
        }

        .table td {
            padding: 15px;
            border-top: 1px solid var(--border-color);
            vertical-align: middle;
        }

        .table tr:hover {
            background-color: #f8f9fa;
        }

        /* 按钮样式 */
        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-weight: 500;
            display: inline-flex;
            align-items: center;
            gap: 5px;
            transition: all 0.3s ease;
        }

        .btn-danger {
            background: var(--danger-color);
            color: white;
        }

        .btn:hover {
            opacity: 0.9;
            transform: translateY(-1px);
        }

        /* 状态标签样式 */
        .status-badge {
            padding: 5px 10px;
            border-radius: 15px;
            font-size: 0.9em;
            font-weight: 500;
        }

        .status-pending {
            background: #f1c40f;
            color: white;
        }

        .status-approved {
            background: var(--success-color);
            color: white;
        }

        .status-rejected {
            background: var(--danger-color);
            color: white;
        }

        /* 加载动画 */
        .loading-spinner {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(255,255,255,0.8);
            z-index: 2000;
            justify-content: center;
            align-items: center;
        }

        .spinner {
            width: 50px;
            height: 50px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid var(--primary-color);
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .table-responsive {
                overflow-x: auto;
            }

            .btn {
                padding: 6px 12px;
            }
        }
    </style>
</head>
<body>
<div class="dashboard">
    <!-- 顶部统计卡片 -->
    <div class="stats-container">
        <div class="stat-card">
            <i class="fas fa-file-alt"></i>
            <div class="stat-info">
                <h3>总请假单数</h3>
                <p id="totalLeaves">0</p>
            </div>
        </div>
        <div class="stat-card">
            <i class="fas fa-clock"></i>
            <div class="stat-info">
                <h3>待审核数</h3>
                <p id="pendingLeaves">0</p>
            </div>
        </div>
        <div class="stat-card">
            <i class="fas fa-check-circle"></i>
            <div class="stat-info">
                <h3>已审核数</h3>
                <p id="approvedLeaves">0</p>
            </div>
        </div>
    </div>

    <!-- 数据表格 -->
    <div class="table-responsive">
        <table id="leaveTable" class="table">
            <thead>
            <tr>
                <th>请假单号</th>
                <th>课程</th>
                <th>事由</th>
                <th>天数</th>
                <th>申请时间</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <!-- 数据将通过JS动态加载 -->
            </tbody>
        </table>
    </div>
</div>

<!-- 加载动画 -->
<div id="loadingSpinner" class="loading-spinner">
    <div class="spinner"></div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {


        // 加载请假单列表
        function loadLeaveList() {
            $.ajax({
                url: '/attend_war/applyLeave',  // 保持原有的URL
                type: 'GET',
                dataType: 'json',
                success: function(data) {
                    // 清空现有的表格数据
                    var tableContent = '';

                    // 判断返回的数据是否有效
                    if (data && Array.isArray(data) && data.length > 0) {
                        // 遍历返回的请假单数据
                        $.each(data, function(index, leave) {
                            var statusText = '';
                            var statusClass = '';
                            var deleteButton = '';

                            if (leave.status === '0') {
                                statusText = '未审核';
                                statusClass = 'status-pending';
                                deleteButton = '<button class="btn btn-danger deleteBtn" data-id="' + leave.leaveID + '">' +
                                    '<i class="fas fa-trash"></i> 删除</button>';
                            } else if (leave.status === '1') {
                                statusText = '审核通过';
                                statusClass = 'status-approved';
                            } else if (leave.status === '2') {
                                statusText = '审核不通过';
                                statusClass = 'status-rejected';
                                deleteButton = '<button class="btn btn-danger deleteBtn" data-id="' + leave.leaveID + '">' +
                                    '<i class="fas fa-trash"></i> 删除</button>';
                            }

                            tableContent += '<tr>';
                            tableContent += '<td>' + leave.leaveID + '</td>';
                            tableContent += '<td>' + leave.courseID + '</td>';
                            tableContent += '<td>' + leave.reason + '</td>';
                            tableContent += '<td>' + leave.daynum + '</td>';
                            tableContent += '<td>' + leave.applytime + '</td>';
                            tableContent += '<td><span class="status-badge ' + statusClass + '">' + statusText + '</span></td>';
                            tableContent += '<td>' + deleteButton + '</td>';
                            tableContent += '</tr>';
                        });

                        // 更新统计信息
                        $("#totalLeaves").text(data.length);
                        $("#pendingLeaves").text(data.filter(item => item.status === '0').length);
                        $("#approvedLeaves").text(data.filter(item => item.status === '1').length);
                    } else {
                        tableContent = '<tr><td colspan="7" class="text-center">没有请假记录。</td></tr>';
                    }

                    // 将数据填充到表格
                    $('#leaveTable tbody').html(tableContent);
                    showLoading(false);
                },
                error: function() {
                    alert('加载请假单列表失败');
                    showLoading(false);
                }
            });
        }

// 删除请假单
        $(document).on('click', '.deleteBtn', function() {
            var leaveID = $(this).data('id');
            var row = $(this).closest('tr');
            if (confirm('确定删除该请假单吗？')) {
                $.ajax({
                    url: "/attend_war/applyLeave?leaveID=" + leaveID,  // 保持原有的URL
                    method: "DELETE",
                    success: function() {
                        alert('删除成功');
                        row.remove();
                        loadLeaveList(); // 重新加载列表以更新统计信息
                    },
                    error: function() {
                        alert('删除失败');
                        showLoading(false);
                    }
                });
            }
        });

        // 首次加载
        loadLeaveList();
    });
</script>
</body>
</html>

